package com.twu.biblioteca;

import com.twu.biblioteca.model.IFactory;
import com.twu.biblioteca.model.WrapperObject;
import com.twu.biblioteca.model.item.ItemFormatter;
import com.twu.biblioteca.model.item.books.Book;
import com.twu.biblioteca.model.LibraryCatalogue;
import com.twu.biblioteca.model.item.books.BookFormatter;
import com.twu.biblioteca.model.menu.Menu;
import com.twu.biblioteca.model.menu.MenuBuilder;
import com.twu.biblioteca.model.menu.MenuItem;
import com.twu.biblioteca.model.item.movies.Movie;
import com.twu.biblioteca.model.item.movies.MovieFormatter;
import com.twu.biblioteca.model.item.movies.Rating;
import com.twu.biblioteca.model.session.Session;
import com.twu.biblioteca.model.session.SessionManager;
import com.twu.biblioteca.model.users.*;
import com.twu.biblioteca.presenter.*;
import com.twu.biblioteca.view.*;
import com.twu.biblioteca.view.utils.InputSystem;
import com.twu.biblioteca.view.utils.OutputSystem;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 08/08/2013
 * Time: 10:52
 * To change this template use File | Settings | File Templates.
 */
public class BibliotecaApp {

    private final LibraryCatalogue libraryCatalogue;
    private final SessionManager sessionManager;
    private final UserManager userManager;

    private final Menu menu;

    private WrapperObject<Boolean> systemExit = new WrapperObject<Boolean>(false);

    public static void main(String[] args){
        BibliotecaApp app = new BibliotecaApp();
        app.connectClient();
    }

    public BibliotecaApp() {
//        context = new ClassPathXmlApplicationContext("beans.xml");
//        systemManager = (SystemManager) context.getBean("systemManager");

        libraryCatalogue = new LibraryCatalogue();
        sessionManager = new SessionManager();
        userManager = new UserManager();

        menu = MenuBuilder.defaultMenu(libraryCatalogue, userManager, sessionManager, systemExit);

        populate();
    }

    public void connectClient(){
        Session session = sessionManager.generateSession();

        Presenter presenter = new MenuPresenter(new MenuView(new OutputSystem(System.out), new InputSystem(System.in)), sessionManager, menu);

        while(!systemExit.getValue()){
            presenter.go(session);
        }
    }

    private void populate(){
        List<Book> collectionOfBooks = Arrays.asList(   new Book("Title 1", "Author 1"),
                                                        new Book("Title 2", "Author 2"));
        List<Movie> collectionOfMovies = Arrays.asList( new Movie("Title 1", 1991, "Director 1", Rating.rating(9)),
                                                        new Movie("Title 2", 2013, "Director 2", Rating.rating(1)),
                                                        new Movie("Title 3", 2000, "Director 3", Rating.unrated()));

        libraryCatalogue.addMultipleBooks(collectionOfBooks);
        libraryCatalogue.addMultipleMovies(collectionOfMovies);
        userManager.add(new User(Username.valueOf(11111111), "pass", "Joe Bloggs", "email@email.com", "00 00 00 00"));
    }
}
